﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Timthoy.Ding
 *      Date:           2013/6/29 23:10:17
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Sector7.Medical.EntityType;
using Sector7.Framework.DB;
using Sector7.Medical.Consts;

namespace Sector7.Medical.DAO.Standard
{
    /// <summary>
    /// <!-- The description about this class. -->
    /// </summary>
    public class SickBedDAO : DAOBase<M7_SickBed>,ISickBedDAO
    {
        #region Public Functions
        /// <summary>
        /// 获取所有病床信息
        /// </summary>
        /// <returns></returns>
        public List<M7_SickBed> ReadAll(int areaId)
        {
            string sql = CreateSelectSql(SELECT_BY_AREA_ID);

            using (DBHelper helper = new DBHelper())
            {
                List<M7_SickBed> bedList = new List<M7_SickBed>();

                SqlDataReader reader = helper.ExecuteQuery(sql,
                    DBUtil.CreateSqlParameter(areaId, "AreaId"));
                while (reader.Read())
                {
                    var bed = new M7_SickBed();
                    DBUtil.ReadData(reader, bed);

                    var bedLevel = new M7_SickBedLevel();
                    DBUtil.ReadData(reader, bedLevel);

                    var room = new M7_SickRoom();
                    DBUtil.ReadData(reader, room);

                    var area = new M7_SickArea();
                    DBUtil.ReadData(reader, area);

                    bed.SickBedLevel = bedLevel;
                    bed.SickRoom = room;
                    bed.SickArea = area;

                    bedList.Add(bed);
                }

                return bedList;
            }
        }

        /// <summary>
        /// 根据病区获取所有病床信息
        /// </summary>
        /// <param name="areaId">病区Id</param>
        /// <returns></returns>
        public List<M7_SickBed> ReadAllByAreaId(int areaId)
        {
            string sql = CreateSelectSql(SELECT_BY_AREA_ID);

            using (DBHelper helper = new DBHelper())
            {
                List<M7_SickBed> bedList = new List<M7_SickBed>();

                SqlDataReader reader = helper.ExecuteQuery(sql,
                    DBUtil.CreateSqlParameter(areaId, "AreaId"));
                while (reader.Read())
                {
                    var bed = new M7_SickBed();
                    DBUtil.ReadData(reader, bed);

                    var bedLevel = new M7_SickBedLevel();
                    DBUtil.ReadData(reader, bedLevel);

                    var room = new M7_SickRoom();
                    DBUtil.ReadData(reader, room);

                    var area = new M7_SickArea();
                    DBUtil.ReadData(reader, area);

                    bed.SickBedLevel = bedLevel;
                    bed.SickRoom = room;
                    bed.SickArea = area;

                    bedList.Add(bed);
                }

                return bedList;
            }
        }

        /// <summary>
        /// 根据病区id和科室id获取所有病床信息
        /// </summary>
        /// <param name="areaId"></param>
        /// <param name="deptId"></param>
        /// <returns></returns>
        public List<M7_SickBed> ReadAllByAreaDeptId(int areaId, int deptId)
        {
            string sql = CreateSelectSql(SELECT_BY_AREA_DEPT_ID);

            using (DBHelper helper = new DBHelper())
            {
                List<M7_SickBed> bedList = new List<M7_SickBed>();

                SqlDataReader reader = helper.ExecuteQuery(sql,
                    DBUtil.CreateSqlParameter(areaId, "AreaId"),
                    DBUtil.CreateSqlParameter(deptId, "DeptId"));

                while (reader.Read())
                {
                    var bed = new M7_SickBed();
                    DBUtil.ReadData(reader, bed);

                    var bedLevel = new M7_SickBedLevel();
                    DBUtil.ReadData(reader, bedLevel);

                    var room = new M7_SickRoom();
                    DBUtil.ReadData(reader, room);

                    var area = new M7_SickArea();
                    DBUtil.ReadData(reader, area);

                    bed.SickBedLevel = bedLevel;
                    bed.SickRoom = room;
                    bed.SickArea = area;

                    bedList.Add(bed);
                }

                return bedList;
            }
        }

        public string CreateSelectSql(int selectType)
        {
            string sql = @"
                SELECT
                    s.BedId,
                    s.BedNumber,
                    s.RoomId,
                    s.BedLevelId,
                    s.AreaId,
                    s.IsValid,
                    s.Remarks,
                    sl.BedLevelId,
                    sl.BedLevelName,
                    sr.RoomId,
                    sr.DeptId,
                    sr.RoomName,
                    sa.AreaId,
                    sa.AreaName
                    FROM M7_SickBed s
                        LEFT JOIN M7_SickBedLevel sl ON sl.BedLevelId = s.BedLevelId
                        LEFT JOIN M7_SickRoom sr ON sr.RoomId = s.RoomId
                        LEFT JOIN M7_SickArea sa ON sa.AreaId = s.AreaId";

            switch (selectType)
            {
                case SELECT_BY_AREA_ID:
                    sql += @"
                WHERE
                    s.AreaId = @AreaId";
                    break;
                case SELECT_BY_AREA_DEPT_ID:
                    sql += @"
                WHERE
                    s.AreaId = @AreaId
                AND
                    sr.DeptId = @DeptId";
                    break;
                default:
                    break;
            }

            return sql;
        }

        /// <summary>
        /// 添加病床信息
        /// </summary>
        /// <param name="area">病床信息</param>
        /// <returns></returns>
        public override object Create(M7_SickBed bed)
        {
            string sql = @"
                INSERT INTO M7_SickBed
                        (BedNumber
                        ,RoomId
                        ,BedLevelId
                        ,IsValid
                        ,Remarks
                        ,AreaId)
                    VALUES
                        (@BedNumber
                        ,@RoomId
                        ,@BedLevelId
                        ,@IsValid
                        ,@Remarks
                        ,@AreaId)
                SELECT @@IDENTITY";

            using (DBHelper helper = new DBHelper())
            {
                return helper.ExecuteScalar(sql,
                    DBUtil.CreateSqlParameter(bed.BedNumber, "BedNumber"),
                    DBUtil.CreateSqlParameter(bed.RoomId, "RoomId"),
                    DBUtil.CreateSqlParameter(bed.BedLevelId, "BedLevelId"),
                    DBUtil.CreateSqlParameter(bed.IsValid, "IsValid"),
                    DBUtil.CreateSqlParameter(bed.Remarks, "Remarks"),
                    DBUtil.CreateSqlParameter(bed.AreaId, "AreaId"));
            }
        }
        /// <summary>
        /// 更新病床信息
        /// </summary>
        /// <param name="employee"></param>
        /// <returns></returns>
        public int Update(M7_SickBed bed)
        {
            string sql = @"
                UPDATE M7_SickBed Set
                        BedNumber = @BedNumber
                        ,RoomId = @RoomId
                        ,BedLevelId = @BedLevelId
                        ,IsValid = @IsValid
                        ,Remarks = @Remarks
                        ,AreaId = @AreaId
                    WHERE BedId = @BedId";

            using (DBHelper helper = new DBHelper())
            {
                return helper.ExecuteNonQuery(sql,
                    DBUtil.CreateSqlParameter(bed.BedNumber, "BedNumber"),
                    DBUtil.CreateSqlParameter(bed.RoomId, "RoomId"),
                    DBUtil.CreateSqlParameter(bed.BedLevelId, "BedLevelId"),
                    DBUtil.CreateSqlParameter(bed.IsValid, "IsValid"),
                    DBUtil.CreateSqlParameter(bed.Remarks, "Remarks"),
                    DBUtil.CreateSqlParameter(bed.AreaId, "AreaId"),
                    DBUtil.CreateSqlParameter(bed.BedId, "BedId"));
            }
        }
        #endregion

        #region Structure
        public SickBedDAO()
        {
            m_tableName = C_M7_TableName.SICK_BED;
            m_primaryKeyName = C_ColumnName.BED_ID;
        }
        #endregion

        #region Inner Member
        private const int SELECT_BY_AREA_ID = 1;

        private const int SELECT_BY_AREA_DEPT_ID = 2;
        #endregion
    }
}
